package org.whuims.core.dataprocessor;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by Qikai on 2016/11/24.
 */
public class DocLoader {
    private String filePath;

    public DocLoader(String filePath) {
        this.filePath = filePath;
    }

    public static void main(String[] args) {
        String filePath = "src/main/webapp/resource/txt/CN-GB/GB 21378-2008.txt";
        DocLoader loader = new DocLoader(filePath);
        loader.load();
    }

    public List<ContentUnit> load() {
        List<String> lines = readLines();
        List<ContentUnit> units = new ArrayList<ContentUnit>();
        for (int i = 0; i < lines.size(); i++) {
            String line = lines.get(i);
            System.out.println(line);
        }
        return units;
    }

    public List<String>[] findMainBody(List<String> lines) {
        Set<String> startSet = new HashSet<String>();
        startSet.add("");
        //
        List<String> preLines = new ArrayList<String>();
        List<String> mainLines = new ArrayList<String>();
        boolean isMainPart = false;
        for (int i = 0; i < lines.size(); i++) {
            String line = lines.get(i);
            if (line.contains("围") || line.contains("总")) {
                if (line.trim().equals("1范围") || line.startsWith("1适用范围") || line.matches("\\s*1\\s*总\\s*则") || line.equals("1主题内容与适用范围") || line.matches("[第1一章编总则范围主题]+")) {
                    isMainPart = true;
                }
            }
            if (isMainPart) {
                mainLines.add(line);
            } else {
                preLines.add(line);
            }
        }
        if (!isMainPart) {
            mainLines = preLines;
        }
        return new List[]{preLines, mainLines};
    }

    public List<String> readLines() {
        List<String> lines = null;
        try {
            File file = new File(this.filePath);
            lines = FileUtils.readLines(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < lines.size() - 1; i++) {
            result.add(lines.get(i));
            if (lines.get(i).matches("=+")) {//如果是换行符，删除
                result.remove(result.size() - 1);
                if (result.get(result.size() - 1).matches("[\\s\\dIVXvx•\\.\\-]+")) {//如果换行符上一行是页码，删除
                    result.remove(result.size() - 1);
                }
                if (lines.get(i + 1).matches("\\s*\\w+[\\/]?\\w*\\s*\\d+.+?")) {//如果换行符下一行是标准标号，删除。
                    i = i + 1;
                }
            }
        }
        lines = new ArrayList<String>();
        for (String line : result) {
//            if(line.contains("范围")){
//                System.out.println(line);
//            }
            if (line.contains("范围")) {
                line = line.replaceAll("\\s*i\\s*适?用?范围", "1范围").replace("\\s*l\\s*适?用?范围", "1范围");//i范围
            }
            lines.add(line);
        }
        return lines;
    }
}
